package com.testin.servlet;

import java.io.IOException;
import java.text.NumberFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@SuppressWarnings("serial")
public abstract class BaseServlet extends HttpServlet {
	private static final String JSP_PREFIX = "/WEB-INF/pages";

	protected void forward(HttpServletRequest request,
			HttpServletResponse response, String path) throws ServletException,
			IOException {
		if (path.endsWith(".jsp")) {
			path = getJspPrefix() + path;
		}

		request.getRequestDispatcher(path).forward(request, response);
	}

	protected void redirect(HttpServletRequest request,
			HttpServletResponse response, String path) throws IOException {
		response.sendRedirect(request.getContextPath() + path);
	}
	
	protected void redirectToHome(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		redirect(request, response, "/");
	}
	
	protected String getJspPrefix() {
		return JSP_PREFIX;
	}
	
	protected String getCurrencyNumber(Number amount) {
		//返回当前默认语言环境的货币格式
		NumberFormat nf = NumberFormat.getCurrencyInstance();
		return nf.format(amount);
	}

}
